package game.phases 
{
	import game.Player;
	/**
	 * ...
	 * @author Sulus Ltd.
	 */
	public class PlanningPhase extends Phase
	{
		private var currentPlayer:Player;
		private var currentPlayerIdx:int;
		
		public function PlanningPhase() 
		{
			
		}
		
		override public function Enter():void 
		{
			papa.gui.SetPhaseName("Planning phase",OnIntroFinish);
		}
		
		private function OnIntroFinish():void 
		{
			papa.mapView.SetArmiesMouseListeners(true);
			
			currentPlayerIdx = 0;
			
			GiveControlToPlayer(currentPlayerIdx);
		}
		
		private function GiveControlToPlayer(idx:int):void
		{
			currentPlayer = papa.players[idx];
			papa.gui.SetCurrentPlayer(papa.players[idx]);
			
			papa.mapView.LockArmies(true, 0, true);
			papa.mapView.LockArmies(false, currentPlayer.uid, false);
			
			papa.gui.ShowEndTurnButton(true,EndTurn);
			
		}
		
		private function EndTurn():void
		{
			papa.gui.SetCurrentPlayer(null);
			papa.gui.ShowEndTurnButton(false, null);
			currentPlayerIdx++;
			if (currentPlayerIdx < papa.players.length)
				GiveControlToPlayer(currentPlayerIdx);
			else
			{
				Finish();
			}
		}
		

		
		override public function Exit():void 
		{
			papa.mapView.LockArmies(false, 0, true);
			papa.mapView.SetArmiesMouseListeners(false);
		}
		
	}

}